// assets/common.js
/**
 \* Created with 微信开发者工具.
 \* @author: 龙威
 \* @time: 2019/10/29 10:14
 \* Description: 
 \*/

import API from "../api/Api"
// const API = require("../api/Api");
const app = getApp();
import { $wuxToast } from '../lib/wuxui/index'

/**
 * 后台服务登录
 */
const serverLogin = () => {
  return new Promise((reslove, reject) => {
    if (app.globalData.userVo && app.globalData.userVo.id) {
      reslove(app.globalData.userVo);
      return;
    }
    // 登录
    wx.login({
      success: res => {
        app.globalData.appInfo.code = res.code;
        let appInfo = app.globalData.appInfo;
        if (!appInfo.code) {
          reject("appInfo: {no code}", appInfo);
          return;
        }
        API.weixin.getOpenId(appInfo).then(res => {
          app.globalData.userVo.weChatOpenId = res.openid;
          API.user.login(app.globalData.userVo).then(res => {
            if (!res) {
              reject(res);
              return;
            }
            app.globalData.userVo = res;
            reslove(res);
          }).catch(e => {
            console.warn(e);
            reject(e);
          });
        }).catch(e => {
          console.warn(e);
          reject(e);
        })
        
      },
      fail: e => {
        reject(e);
      }
    });
  });
}

/**
 * 后台服务注册
 */
const serverRegister = ({ phone, validateCode }) => {
  app.globalData.userVo.phone = phone;
  app.globalData.userVo.validateCode = validateCode;
  //如果有openid则表面已经wx.login允许了，可以直接调注册接口，如果没有则得再次wx.login一次
  if (app.globalData.userVo && app.globalData.userVo.weChatOpenId) {
    return new Promise((reslove, reject) => {
      API.user.register(app.globalData.userVo).then(res => {
        if (!res) {
          reject(res);
          return;
        }
        app.globalData.userVo = res;
        reslove(res);
      }).catch(e => {
        console.warn(e);
        reject(e);
      });
    });
  } else {
    // 登录
    wx.login({
      success: res => {
        app.globalData.appInfo.code = res.code;
        let appInfo = app.globalData.appInfo;
        if (!appInfo.code) {
          reject("appInfo: {no code}", appInfo);
          return;
        }
        API.weixin.getOpenId(appInfo).then(res => {
          app.globalData.userVo.weChatOpenId = res.openid;
          API.user.register(app.globalData.userVo).then(res => {
            if (!res) {
              reject(res);
              return;
            }
            app.globalData.userVo = res;
            reslove(res);
          }).catch(e => {
            console.warn(e);
            reject(e);
          });
        }).catch(e => {
          console.warn(e);
          reject(e);
        })
      },
      fail: e => {
        reject(e);
      }
    });
  }
}

const showToast = (type, text, successFunc) => {
  $wuxToast().show({
    type: type,
    duration: 1500,
    color: '#fff',
    mask: false,
    text: text,
    success: () => {
      if (successFunc && "function" === typeof successFunc) {
        successFunc();
      }
    }
  })
}

export default {
  serverLogin,
  serverRegister,
  showToast
}